<?php
declare(strict_types=1);

namespace Service\Consumer\YunCangConsumer;
use Service\BaseService;
use Service\Code\Normal;
use App\Model\BbcLogModel\GoodsRevLog;

/**
 * 云仓商品消费中心
 * @author chenshubo
 */
class YunCangCommodityMessageConsumerService extends BaseService
{
    //消息服务类路径
    protected $message_router = "Service\\Consumer\\YunCangConsumer\\Router\\";
    
    /**
     * 主数据消费
     * @param int $sid
     * @param int $cid
     * @param string $messageType
     * @param array $data
     * @param string $messageId
     * @param int $IsFinished
     */
    public function consumer(int $sid,int $cid,string $messageType,array $data,string $messageId,int $IsFinished)
    {
        $class_name = $this->message_router . $messageType;
        if( !class_exists($class_name) ) {
            return Normal::returnCode(Normal::FAIL,"消息服务类{$class_name}不存在!");
        }
        
        $_function = __FUNCTION__;
        if( !method_exists($class_name,$_function) ) {
            return Normal::returnCode(Normal::FAIL,"消息服务类{$class_name},方法{$_function}不存在!");
        }
        
        $result = $class_name::instance()->{$_function}($sid,$cid,$data,$messageId,$IsFinished);
        if( $messageType == 'goodsChange' ) 
        {
            $_insert_logs = [];
            foreach ($data as $goods)
            {
                $sku = current($goods['skuList']);
                $_insert_logs[] = [
                    'cid'           => $cid,
                    'message_type'  => $messageType,
                    'skucode'       => $sku['skucode'],
                    'rev_status'    => $result['code'],
                    'rev_result'    => json_encode($result,JSON_UNESCAPED_UNICODE),
                    'contents'      => json_encode($goods,JSON_UNESCAPED_UNICODE),
                    'created_at'    => date("Y-m-d H:i:s")
                ];
            }
        } 
        else if( $messageType == 'goodsSkuChange' )
        {
            $_insert_logs = [];
            $data['skuList'] = (array)$data['skuList'];
            foreach ($data['skuList'] as $sku)
            {
                $_insert_logs[] = [
                    'cid'           => $cid,
                    'message_type'  => $messageType,
                    'skucode'       => $sku,
                    'rev_status'    => $result['code'],
                    'rev_result'    => json_encode($result,JSON_UNESCAPED_UNICODE),
                    'contents'      => json_encode($data,JSON_UNESCAPED_UNICODE),
                    'created_at'    => date("Y-m-d H:i:s")
                ];
            }
        }
        else
        {
            $_insert_logs = [
                'cid'           => $cid,
                'message_type'  => $messageType,
                'skucode'       => 0,
                'rev_status'    => $result['code'],
                'rev_result'    => json_encode($result,JSON_UNESCAPED_UNICODE),
                'contents'      => json_encode($data,JSON_UNESCAPED_UNICODE),
                'created_at'    => date("Y-m-d H:i:s")
            ];
        }
        GoodsRevLog::instance()->insert($_insert_logs);        
        return $result;
    }
}